package com.slofzx.nuoda.evaluation.service.impl;

import com.slofzx.nuoda.common.config.SystemConfig;
import com.slofzx.nuoda.common.dao.AbstractCompanyDAO;
import com.slofzx.nuoda.common.entity.SysUser;
import com.slofzx.nuoda.common.service.impl.CompanyServiceImpl;
import com.slofzx.nuoda.common.shiro.Principal;
import com.slofzx.nuoda.common.vo.CompanyVO;
import com.slofzx.nuoda.evaluation.enums.CustomOfficeTypeEnum;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 覆写原来逻辑，支持大庆需求
 * @author sunfuyong
 */
@Primary
@Service
public class CustomCompanyServiceImpl extends CompanyServiceImpl {
    @Override
    public List<CompanyVO> getUserCompanyTree() {
//        SysUserVO user = getUser();
//        String userId = passportService.getUserId();
        String userId = null;
        Principal principal = (Principal) SecurityUtils.getSubject().getPrincipal();
        if(principal!=null) {
            userId = principal.getId();
        }
        SysUser user = sysUserDAO.selectById(userId);
        String targetCompanyId = user.getCompanyId();
        if(Objects.equals(user.getCompanyId(),systemConfig.getOfficeRoot()) == false) {
            CompanyVO vo = get(user.getCompanyId());
            if(vo.getType() != null && CustomOfficeTypeEnum.getEnum(vo.getType()) == CustomOfficeTypeEnum.RESEARCH){
                targetCompanyId = systemConfig.getOfficeRoot();
            }
        }
        return getCompanyTree(targetCompanyId);
    }
}
